package com.example.knowledgeget1.ChatController;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import com.example.knowledgeget1.ChatCommon.Request.ChatRequest;
import com.example.knowledgeget1.Util.RelationResponseLexer;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class RelationChatController {
    @PostMapping("/Rchat")
    public void chat() throws IOException {
        String url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
        String ApiKey = "sk-6b435da2f9c743e38a1aeeb1b6bdca85";
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:8080/api/nodetypes", String.class);
        // 创建一个ObjectMapper实例
        ObjectMapper objectMapper = new ObjectMapper();

        // 使用ObjectMapper将response.getBody()转换为你期望的类型
        List<LinkedHashMap<String, Object>> nodeTypes = objectMapper.readValue(response.getBody(), new TypeReference<List<LinkedHashMap<String, Object>>>(){});
        ArrayList<String> names = new ArrayList<>();
        //建立知识点列表
        for (LinkedHashMap<String, Object> nodeType1 : nodeTypes) {
            String name = (String) nodeType1.get("name");
            names.add(name);
        }
        // 访问大模型
        for (LinkedHashMap<String, Object> nodeType : nodeTypes) {
            // Get the name field
            String name = (String) nodeType.get("name");
            ArrayList<String> newNames = new ArrayList<>();
            for (String name1 : names) {
                if (!name1.equals(name)) {
                    newNames.add(name1);
                }
            }

            String question = String.format("请根据以下JavaScript知识点列表：%s，识别哪些是学习'JavaScript的%s'的直接前置知识点，并以斜杠分隔直接列出这些知识点名称，不包括其他描述性内容，如 知识点1/知识点2/知识点3。如果该知识点无前置知识点，则输出无，不包括其他描述性内容。",newNames, name);
            ChatRequest chatRequest = new ChatRequest(question);
            String json = JSONUtil.toJsonStr(chatRequest);

            // Send request to the model and get the response
            String result = HttpRequest.post(url)
                    .header("Authorization", "Bearer " + ApiKey)
                    .header("Content-Type", "application/json")
                    .body(json)
                    .execute().body();

            RelationResponseLexer.Lexer(name, result, newNames);

            //休眠10s
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Chat completed.");
    }
}
